package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.ShopCarDao;
import com.baichang.taoshunda.entity.ShopCar;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 17/08/23.
 */
@Repository
public class ShopCarDaoImpl extends BaseDaoImpl<ShopCar> implements ShopCarDao {
    /**
     * 删除所有购物车
     *
     * @param userId 用户编号
     * @param bussId
     * @return
     */
    @Override
    public boolean deleteShopCarById(Integer userId, Integer bussId) {
        return delete("user_id=? ", userId) >= 0;
    }

    @Override
    public boolean deleteShopCarByBussId(Integer userId, Integer bussId) {
        return delete("user_id=? and buss_id=?", userId, bussId) > 0;
    }


    /**
     * 修改购物车里的商品数量
     *
     * @param id     购物车编号
     * @param number 商品数量
     * @return 修改成功/失败
     */
    @Override
    public int updateCarNumber(Integer id, Integer number) {
        String sql = " UPDATE {table} SET number=? WHERE id=? ";
        return update(sql, number, id);
    }

    /**
     * 查询用户在商家的购物车
     *
     * @param userId 用户编号
     * @param bussId 商家编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findShopCar(Integer userId, Integer bussId) {
        String sql = "SELECT g.`name` AS goodsName, g.is_free AS isFree, g.is_bespeak AS isBespeak,g.unit,sc.goods_price, g.is_home_installation AS isHomeInstallation, g.is_no_reason_return AS isNoReasonReturn , g.head_pic AS headPic, sc.id AS carId, sc.user_id, sc.goods_id AS goodsId, g.price , sc.buss_id, sc.goods_spec, sc.number AS goodsNumber FROM t_shop_car sc INNER JOIN t_goods g ON g.id = sc.goods_id WHERE sc.user_id = ? AND sc.buss_id = ?";
        return queryForMapList(sql, userId, bussId);
    }

    @Override
    public int updateShopCar(Integer carId, Integer userId, Integer bussId, Integer goodsId, Integer number, String goodsSpec) {
//        String sql = "UPDATE `t_shop_car` SET `user_id`=?, `goods_id`=?, `buss_id`=?, `goods_spec`=?, `number`=? WHERE (`id`=?)";
        String sql = "UPDATE `t_shop_car` SET `number`=? WHERE (`id`=?)";
        return update(sql, number, carId);
//        return update(sql, userId, goodsId, bussId, goodsSpec, number, carId);
    }

    @Override
    public boolean removeAllCar(Integer userId) {
        return delete("user_id=? ", userId) >= 0;
    }

    /**
     * 查询商品数量  及金额
     *
     * @param userId
     * @param bussId
     * @return
     */
    @Override
    public Map<String, Object> getAllNumberAndAllMoney(Integer userId, Integer bussId) {
        String sql = "SELECT IFNULL(SUM(sc.number), 0) AS allNumber , IFNULL(SUM(sc.number * sc.goods_price), 0) AS allPayMoney FROM t_shop_car sc INNER JOIN t_goods g ON g.id = sc.goods_id WHERE sc.user_id = ? AND sc.buss_id = ?";
        return queryForMap(sql, userId, bussId);
    }

    @Override
    public int getCountByUserIdAndGoodsId(Integer goodsId, int userId) {
        String sql = "SELECT SUM(number) FROM t_shop_car  WHERE goods_id=? AND user_id=?";
        return queryForInt(sql, goodsId, userId);
    }

    /**
     * 根据商品编号、用户编号获取用户的购物车列表
     *
     * @param userId  用户编号
     * @param goodsId 商品编号
     * @return 购物车列表
     */
    @Override
    public List<ShopCar> findCarByGoodsAndUserId(Integer userId, Integer goodsId) {
        String sql = " SELECT * FROM {table} WHERE user_id=? AND goods_id=? ";
        return queryForBeanList(sql, userId, goodsId);
    }
}
